<?php


namespace Admin\Controller;

class GoodsbrandController extends AdminController {


    public function index(){
        $goodsbrand = D('Goodsbrand');

        $infos =  $this->lists($goodsbrand->order('rank'),array(),null);
//        $infos = $goodstype->field(true)->select();
//        $infos = D('Common/Tree')->toFormatTree($infos);
//            dump($infos);

        $this->assign('infos',$infos);
        $this->display();
    }

    public function add(){
        $goodsbrand = D('Goodsbrand');
        $goodstype = D('Goodstype');

        if(IS_POST){
//            dump($_POST);
//            $logo_path = I('post.logo_path',0);
//            $logo_savename = I('post.logo_savename',0);
//            if(strpos($logo_path,$logo_savename)){
//                $logo_path = str_replace($logo_savename,'',$logo_path);
//                $logo_path = str_replace('/fanlei','',$logo_path);
//            }
//            import('ORG.Util.Image.ThinkImage');
//            $image = new \ThinkImage();
//
//            dump($logo_path);
//            $image->open($logo_path.$logo_savename);
//            $image->thumb(150,100,THINKIMAGE_THUMB_SCALING)->save($logo_path.'samll'.$logo_savename);
//
//            echo __APP__;
//            exit();
            $z = $goodsbrand->toadd($_POST);
            if($z){
//                dump($z);
                $this->success('添加成功',U('Goodsbrand/index'));
            }else{
                $this->success('添加失败',U('Goodsbrand/add'));
            }
        }else{
            $menus = $goodstype->field(true)->select();
            $menus = D('Common/Tree')->toFormatTree($menus);
//            dump($menus);
            $menus = array_merge(array(0=>array('id'=>0,'name'=>'顶级菜单')), $menus);
            $this->assign('Menus', $menus);
            $this->display();
        }

    }

    public function upd(){
        $goodsbrand = D('Goodsbrand');
        $goodstype = D('Goodstype');
        if(IS_POST){
//dump($_POST);
            $z =  $goodsbrand->upd($_POST);
            if($z){
                $this->success('更新成功', U('index'));
            }else{
                $this->error('更新失败', U('Goodsbrand/upd',array('id'=>$_POST['id'])));
            }
        }else{

            $info = $goodsbrand->find(I(id,"0"));
//            dump($info);
            $menus = $goodstype->field(true)->select();
            $menus = D('Common/Tree')->toFormatTree($menus);
//            dump($menus);
            $menus = array_merge(array(0=>array('id'=>0,'name'=>'顶级菜单')), $menus);
            $this->assign('Menus', $menus);

            $this->assign("info",$info);
            $this->display();
        }

    }

    public function del($id = null){
        $goodsbrand = D('Goodsbrand');

        $z =  $goodsbrand->del($id);
        if($z){
            $this->success('删除成功', U('index'));
        }else{
            $this->error('删除失败', U('index'));
        }
    }

    public function setrec($id = null,$recommend = 0){
        $goodsbrand = D('Goodsbrand');

        if(IS_POST){
            $z = $goodsbrand->rec($id,$recommend);
        }else{
//            dump(I('get.id'));
            $z = $goodsbrand->rec(I('get.id'),I('get.recommend'));
        }
//
        if($z){
            $this->success('成功', U('index'));
        }else{
            $this->error('失败', U('index'));
        }

    }
    //排序
    public function setrank(){

//        dump(I('rank'));
        $goodstype = D('Goodsbrand');
        $z = $goodstype->where("id =".I('id'))->setField('rank',I('rank'));

        //无论成功与否都返回index
        if($z){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }

    }



}
